/*
 * my_info_cb.c
 *
 *  Created on: 2013-2-5
 *      Author: panqiuhui
 */
#include <stdlib.h>
#include <string.h>
#include "../head/gui_callback/my_info_cb.h"
#include "../head/common_struct/userinfo.h"
#include "../head/communicate_with_server/userinfo_transfer.h"
#include "../head/gui_callback/global_cb.h"

extern GtkBuilder *builder;

//处理工具栏中的个人资料的点击事件
void menuitem_personal_info_activate_cb(GtkMenuItem *menuitem,
		gpointer user_data) {
	GtkLabel *label_personal_info_basic_username;
	GtkLabel *label_personal_info_detail_name;
	GtkLabel *label_personal_info_detail_gender;
	GtkLabel *label_personal_info_detail_birthday;
	GtkLabel *label_personal_info_detail_state;
	GtkLabel *label_personal_info_detail_certificate_type;
	GtkLabel *label_personal_info_detail_certificate_id;
	GtkLabel *label_personal_info_contact_moblie_phone;
	GtkLabel *label_personal_info_contact_fixed_telephone;
	GtkLabel *label_personal_info_contact_email;
	GtkLabel *label_personal_info_contact_address;
	GtkLabel *label_personal_info_contact_postcode;

	label_personal_info_basic_username =
			GTK_LABEL(gtk_builder_get_object(builder, "label_personal_info_basic_username"));
	label_personal_info_detail_name =
			GTK_LABEL(gtk_builder_get_object(builder, "label_personal_info_detail_name"));
	label_personal_info_detail_gender =
			GTK_LABEL(gtk_builder_get_object(builder, "label_personal_info_detail_gender"));
	label_personal_info_detail_birthday =
			GTK_LABEL(gtk_builder_get_object(builder, "label_personal_info_detail_birthday"));
	label_personal_info_detail_state =
			GTK_LABEL(gtk_builder_get_object(builder, "label_personal_info_detail_state"));
	label_personal_info_detail_certificate_type =
			GTK_LABEL(gtk_builder_get_object(builder, "label_personal_info_detail_certificate_type"));
	label_personal_info_detail_certificate_id =
			GTK_LABEL(gtk_builder_get_object(builder, "label_personal_info_detail_certificate_id"));
	label_personal_info_contact_moblie_phone =
			GTK_LABEL(gtk_builder_get_object(builder, "label_personal_info_contact_moblie_phone"));
	label_personal_info_contact_fixed_telephone =
			GTK_LABEL(gtk_builder_get_object(builder, "label_personal_info_contact_fixed_telephone"));
	label_personal_info_contact_email =
			GTK_LABEL(gtk_builder_get_object(builder, "label_personal_info_contact_email"));
	label_personal_info_contact_address =
			GTK_LABEL(gtk_builder_get_object(builder, "label_personal_info_contact_address"));
	label_personal_info_contact_postcode =
			GTK_LABEL(gtk_builder_get_object(builder, "label_personal_info_contact_postcode"));

	struct userinfo *user = get_user_info();
	gtk_label_set_label(label_personal_info_basic_username, user->username);
	gtk_label_set_label(label_personal_info_detail_name, user->name);
	gtk_label_set_label(label_personal_info_detail_gender, user->gender);
	gtk_label_set_label(label_personal_info_detail_birthday, user->birthday);
	gtk_label_set_label(label_personal_info_detail_state, user->country);
	gtk_label_set_label(label_personal_info_detail_certificate_type,
			user->creditType);
	gtk_label_set_label(label_personal_info_detail_certificate_id,
			user->creditID);
	gtk_label_set_label(label_personal_info_contact_moblie_phone,
			user->mobilephoneNum);
	gtk_label_set_label(label_personal_info_contact_fixed_telephone,
			user->fixedphoneNum);
	gtk_label_set_label(label_personal_info_contact_email, user->email);
	gtk_label_set_label(label_personal_info_contact_address, user->address);
	gtk_label_set_label(label_personal_info_contact_postcode, user->postcode);

	window_show(MY_INFO_PERSONAL_INFO);
}

//处理编辑个人资料按钮的点击事件
void button_edit_personal_info_clicked_cb(GtkButton *button, gpointer user_data) {
	GtkLabel *label_edit_personal_info_basic_username;
	GtkEntry *entry_edit_personal_info_detail_name;
	GtkRadioButton *radiobutton_edit_personal_info_detail_gender_male;
	GtkRadioButton *radiobutton_edit_personal_info_detail_gender_female;
	GtkEntry *entry_edit_personal_info_detail_birthday;
	GtkCalendar *calendar_edit_personal_info_detail_birthday;
	GtkComboBox *combobox_edit_personal_info_detail_state;
	GtkComboBox *combobox_edit_personal_info_detail_certificate_type;
	GtkEntry *entry_edit_personal_info_detail_certificate_id;
	GtkEntry *entry_edit_personal_info_contact_mobile_phone;
	GtkEntry *entry_edit_personal_info_contact_fixed_telephone;
	GtkEntry *entry_edit_personal_info_contact_email;
	GtkEntry *entry_edit_personal_info_contact_address;
	GtkEntry *entry_edit_personal_info_contact_postcode;

	label_edit_personal_info_basic_username =
			GTK_LABEL(gtk_builder_get_object(builder,"label_edit_personal_info_basic_username"));
	entry_edit_personal_info_detail_name =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_edit_personal_info_detail_name"));
	radiobutton_edit_personal_info_detail_gender_male =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder, "radiobutton_edit_personal_info_detail_gender_male"));
	radiobutton_edit_personal_info_detail_gender_female =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder, "radiobutton_edit_personal_info_detail_gender_female"));
	entry_edit_personal_info_detail_birthday =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_edit_personal_info_detail_birthday"));
	calendar_edit_personal_info_detail_birthday =
			GTK_CALENDAR(gtk_builder_get_object(builder,"calendar_edit_personal_info_detail_birthday"));
	combobox_edit_personal_info_detail_state =
			GTK_COMBO_BOX(gtk_builder_get_object(builder, "combobox_edit_personal_info_detail_state"));
	combobox_edit_personal_info_detail_certificate_type =
			GTK_COMBO_BOX(gtk_builder_get_object(builder, "combobox_edit_personal_info_detail_certificate_type"));
	entry_edit_personal_info_detail_certificate_id =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_edit_personal_info_detail_certificate_id"));
	entry_edit_personal_info_contact_mobile_phone =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_edit_personal_info_contact_mobile_phone"));
	entry_edit_personal_info_contact_fixed_telephone =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_edit_personal_info_contact_fixed_telephone"));
	entry_edit_personal_info_contact_email =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_edit_personal_info_contact_email"));
	entry_edit_personal_info_contact_address =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_edit_personal_info_contact_address"));
	entry_edit_personal_info_contact_postcode =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_edit_personal_info_contact_postcode"));

	struct userinfo *user = get_user_info();
	gtk_label_set_label(label_edit_personal_info_basic_username,
			user->username);
	gtk_entry_set_text(entry_edit_personal_info_detail_name, user->name);
	if (strcmp(user->gender, "男") == 0) {
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
				radiobutton_edit_personal_info_detail_gender_male), true);
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
				radiobutton_edit_personal_info_detail_gender_female), false);
	} else {
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
				radiobutton_edit_personal_info_detail_gender_female), true);
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
				radiobutton_edit_personal_info_detail_gender_male), false);
	}

	gtk_entry_set_text(entry_edit_personal_info_detail_birthday,
			user->birthday);
	guint year;
	guint month;
	guint day;
	sscanf(user->birthday, "%04d-%02d-%02d", &year, &month, &day);
	gtk_calendar_select_month(calendar_edit_personal_info_detail_birthday,
			--month, year);
	gtk_calendar_select_day(calendar_edit_personal_info_detail_birthday, day);

	//设置国家的combobox
	GtkTreeModel *model_country = gtk_combo_box_get_model(
			combobox_edit_personal_info_detail_state);
	GtkTreeIter iter_country;
	gboolean valid_country;
	gchar *country;
	gint index_country = 0;

	valid_country = gtk_tree_model_get_iter_first(model_country, &iter_country);
	while (valid_country) {
		index_country++;
		gtk_tree_model_get(model_country, &iter_country, 0, &country, -1);
		if (strcmp(user->country, country) == 0) {
			break;
		}
		valid_country = gtk_tree_model_iter_next(model_country, &iter_country);
	}

	gtk_combo_box_set_active(combobox_edit_personal_info_detail_state,
			index_country - 1);

	//设置证件类别的combobox
	GtkTreeModel *model_credit_type = gtk_combo_box_get_model(
			combobox_edit_personal_info_detail_certificate_type);
	GtkTreeIter iter_credit_type;
	gboolean valid_credit_type;
	gchar *credit_type;
	gint index_credit_type = 0;

	valid_credit_type = gtk_tree_model_get_iter_first(model_credit_type,
			&iter_credit_type);
	while (valid_credit_type) {
		index_credit_type++;
		gtk_tree_model_get(model_credit_type, &iter_credit_type, 0,
				&credit_type, -1);
		if (strcmp(user->creditType, credit_type) == 0) {
			break;
		}
		valid_credit_type = gtk_tree_model_iter_next(model_credit_type,
				&iter_credit_type);
	}

	gtk_combo_box_set_active(
			combobox_edit_personal_info_detail_certificate_type,
			index_credit_type - 1);

	gtk_entry_set_text(entry_edit_personal_info_detail_certificate_id,
			user->creditID);
	gtk_entry_set_text(entry_edit_personal_info_contact_mobile_phone,
			user->mobilephoneNum);
	gtk_entry_set_text(entry_edit_personal_info_contact_fixed_telephone,
			user->fixedphoneNum);
	gtk_entry_set_text(entry_edit_personal_info_contact_email, user->email);
	gtk_entry_set_text(entry_edit_personal_info_contact_address, user->address);
	gtk_entry_set_text(entry_edit_personal_info_contact_postcode,
			user->postcode);
	window_show(MY_INFO_EDIT_PERSONAL_INFO);
}

//处理编辑个人资料提交按钮的点击事件
void button_edit_personal_info_submit_clicked_cb(GtkButton *button,
		gpointer user_data) {
	GtkLabel *label_edit_personal_info_basic_username;
	GtkEntry *entry_edit_personal_info_detail_name;
	GtkRadioButton *radiobutton_edit_personal_info_detail_gender_male;
	GtkEntry *entry_edit_personal_info_detail_birthday;
	GtkComboBox *combobox_edit_personal_info_detail_state;
	GtkComboBox *combobox_edit_personal_info_detail_certificate_type;
	GtkEntry *entry_edit_personal_info_detail_certificate_id;
	GtkEntry *entry_edit_personal_info_contact_mobile_phone;
	GtkEntry *entry_edit_personal_info_contact_fixed_telephone;
	GtkEntry *entry_edit_personal_info_contact_email;
	GtkEntry *entry_edit_personal_info_contact_address;
	GtkEntry *entry_edit_personal_info_contact_postcode;
	GtkWindow *global_window;

	global_window =
			GTK_WINDOW(gtk_builder_get_object(builder, "global_window"));

	label_edit_personal_info_basic_username =
			GTK_LABEL(gtk_builder_get_object(builder,"label_edit_personal_info_basic_username"));
	entry_edit_personal_info_detail_name =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_edit_personal_info_detail_name"));
	radiobutton_edit_personal_info_detail_gender_male =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder, "radiobutton_edit_personal_info_detail_gender_male"));
	entry_edit_personal_info_detail_birthday =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_edit_personal_info_detail_birthday"));
	combobox_edit_personal_info_detail_state =
			GTK_COMBO_BOX(gtk_builder_get_object(builder, "combobox_edit_personal_info_detail_state"));
	combobox_edit_personal_info_detail_certificate_type =
			GTK_COMBO_BOX(gtk_builder_get_object(builder, "combobox_edit_personal_info_detail_certificate_type"));
	entry_edit_personal_info_detail_certificate_id =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_edit_personal_info_detail_certificate_id"));
	entry_edit_personal_info_contact_mobile_phone =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_edit_personal_info_contact_mobile_phone"));
	entry_edit_personal_info_contact_fixed_telephone =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_edit_personal_info_contact_fixed_telephone"));
	entry_edit_personal_info_contact_email =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_edit_personal_info_contact_email"));
	entry_edit_personal_info_contact_address =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_edit_personal_info_contact_address"));
	entry_edit_personal_info_contact_postcode =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_edit_personal_info_contact_postcode"));

	const gchar *username = gtk_label_get_label(
			label_edit_personal_info_basic_username);

	const gchar *name = gtk_entry_get_text(
			entry_edit_personal_info_detail_name);
	int name_length = g_utf8_strlen(name, NAME_LEN);
	if (name_length == 0) { //检查姓名是否为空
		gtk_show_warning(global_window, "%s", "姓名不能为空", "输入提醒");
		return;
	}

	gboolean ismale = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			radiobutton_edit_personal_info_detail_gender_male) );

	const gchar *birthday = gtk_entry_get_text(
			entry_edit_personal_info_detail_birthday);

	const gchar *country;
	GtkTreeModel *model_country = gtk_combo_box_get_model(
			combobox_edit_personal_info_detail_state);
	GtkTreeIter iter_country;
	gtk_combo_box_get_active_iter(combobox_edit_personal_info_detail_state,
			&iter_country);
	gtk_tree_model_get(model_country, &iter_country, 0, &country, -1);

	const gchar *credit_type;
	GtkTreeModel *model_credit_type = gtk_combo_box_get_model(
			combobox_edit_personal_info_detail_certificate_type);
	GtkTreeIter iter_credit_type;
	gtk_combo_box_get_active_iter(
			combobox_edit_personal_info_detail_certificate_type,
			&iter_credit_type);
	gtk_tree_model_get(model_credit_type, &iter_credit_type, 0, &credit_type,
			-1);

	const gchar *creditID = gtk_entry_get_text(
			entry_edit_personal_info_detail_certificate_id);
	int creditID_length = g_utf8_strlen(creditID, CREDIT_ID_LEN);
	if (creditID_length == 0) { //检查证件号码是否为空
		gtk_show_warning(global_window, "%s", "证件号码不能为空", "输入提醒");
		return;
	}

	const gchar *mobilephone_num = gtk_entry_get_text(
			entry_edit_personal_info_contact_mobile_phone);
	int mobilephone_num_length = g_utf8_strlen(mobilephone_num,
			MOBILE_PHONE_NUM_LEN);
	if (mobilephone_num_length == 0) { //检查手机号码是否为空
		gtk_show_warning(global_window, "%s", "手机号码不能为空", "输入提醒");
		return;
	}

	const gchar *fixedphone_num = gtk_entry_get_text(
			entry_edit_personal_info_contact_fixed_telephone);

	const gchar *email = gtk_entry_get_text(
			entry_edit_personal_info_contact_email);
	int email_length = g_utf8_strlen(email, EMAIL_LEN);
	if (email_length == 0) { //检查电子邮件是否为空
		gtk_show_warning(global_window, "%s", "电子邮件不能为空", "输入提醒");
		return;
	}

	const gchar *address = gtk_entry_get_text(
			entry_edit_personal_info_contact_address);
	const gchar *postcode = gtk_entry_get_text(
			entry_edit_personal_info_contact_postcode);

	struct userinfo user;
	memset(&user, 0, sizeof(user));
	int username_length = g_utf8_strlen(username, USERNAME_LEN);
	g_utf8_strncpy(user.username, username, username_length);

	g_utf8_strncpy(user.name, name, name_length);

	if (ismale) {
		int male_length = g_utf8_strlen("男", GENDER_LEN);
		g_utf8_strncpy(user.gender, "男", male_length);
	} else {
		int female_length = g_utf8_strlen("女", GENDER_LEN);
		g_utf8_strncpy(user.gender, "女", female_length);
	}

	strncpy(user.birthday, birthday, BIRTHDAY_LEN);

	int country_length = g_utf8_strlen(country, COUNTRY_LEN);
	g_utf8_strncpy(user.country, country, country_length);

	int credit_type_length = g_utf8_strlen(credit_type, CREDIT_TYPE_LEN);
	g_utf8_strncpy(user.creditType, credit_type, credit_type_length);

	g_utf8_strncpy(user.creditID, creditID, creditID_length);

	g_utf8_strncpy(user.mobilephoneNum, mobilephone_num,
			mobilephone_num_length);

	int fixedphone_num_length = g_utf8_strlen(fixedphone_num,
			FIXED_PHONE_NUM_LEN);
	g_utf8_strncpy(user.fixedphoneNum, fixedphone_num, fixedphone_num_length);

	g_utf8_strncpy(user.email, email, email_length);

	int address_length = g_utf8_strlen(address, ADDRESS_LEN);
	g_utf8_strncpy(user.address, address, address_length);

	int postcode_length = g_utf8_strlen(postcode, POSTCODE_LEN);
	g_utf8_strncpy(user.postcode, postcode, postcode_length);

	struct userinfo_request request;
	memset(&request, 0, sizeof(request));
	request.user = user;
	bool result = edit_personial_info(&request);
	if (result) {
		gtk_show_info(global_window, "%s", "保存成功", "信息提示");
		menuitem_personal_info_activate_cb(NULL, NULL ); //跳转到个人资料界面
	} else {
		gtk_show_warning(global_window, "%s", "保存失败，请重试", "信息提示");
	}
}

//处理工具栏中的密码修改的点击事件
void menuitem_change_password_activate_cb(GtkMenuItem *menuitem,
		gpointer user_data) {
	GtkEntry *entry_changepw_originalpw;
	GtkEntry *entry_changepw_newpw;
	GtkEntry *entry_changepw_pwagain;

	entry_changepw_originalpw =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_changepw_originalpw"));
	entry_changepw_newpw =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_changepw_newpw"));
	entry_changepw_pwagain =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_changepw_pwagain"));

	gtk_entry_set_text(entry_changepw_originalpw, "");
	gtk_entry_set_text(entry_changepw_newpw, "");
	gtk_entry_set_text(entry_changepw_pwagain, "");

	window_show(MY_INFO_CHANGE_PASSWORD);
}

//处理密码修改提交按钮的点击事件
void button_change_password_ok_clicked_cb(GtkButton *button, gpointer user_data) {
	GtkEntry *entry_changepw_originalpw;
	GtkEntry *entry_changepw_newpw;
	GtkEntry *entry_changepw_pwagain;
	GtkWindow *global_window;

	global_window =
			GTK_WINDOW(gtk_builder_get_object(builder, "global_window"));
	entry_changepw_originalpw =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_changepw_originalpw"));
	entry_changepw_newpw =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_changepw_newpw"));
	entry_changepw_pwagain =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_changepw_pwagain"));

	const gchar *originalpw = gtk_entry_get_text(entry_changepw_originalpw);
	int originalpw_len = strlen(originalpw);
	if (originalpw_len == 0) {
		gtk_show_warning(global_window, "%s", "原密码不能为空", "输入提醒");
		return;
	}
	const gchar *newpw = gtk_entry_get_text(entry_changepw_newpw);
	int newpw_len = strlen(newpw);
	if (newpw_len == 0) {
		gtk_show_warning(global_window, "%s", "新密码不能为空", "输入提醒");
		return;
	} else if (newpw_len < 6) {
		gtk_show_warning(global_window, "%s", "新密码不能少于6个字符", "输入提醒");
		return;
	}
	const gchar *pw_again = gtk_entry_get_text(entry_changepw_pwagain);
	if (strcmp(newpw, pw_again) != 0) {
		gtk_show_warning(global_window, "%s", "两次输入的密码不一致", "输入提醒");
		return;
	}

	struct userinfo *user = get_user_info();
	struct change_pw_request request;
	memset(&request, 0, sizeof(request));

	strncpy(request.username, user->username, strlen(user->username));
	strncpy(request.original_pw, originalpw, originalpw_len);
	strncpy(request.new_pw, newpw, newpw_len);

	bool result = change_password(&request);

	if (result) {
		gtk_show_info(global_window, "%s", "密码更改成功", "信息提示");
		menuitem_change_password_activate_cb(NULL, NULL );
	} else {
		gtk_show_warning(global_window, "%s", "原密码错误", "信息提示");
	}

}
